package instruments;

import data.NavmeshRegion;
import data.FPoint;
import data.InstrumentGraphics;
import java.awt.Color;
import java.util.List;
import pathfind.GlobalPathFinder;

/**
 * @author Dmitry
 */
public class GlobalPathInstrument extends AbstractPolygonInstrument {
    private NavmeshRegion p1;
    private NavmeshRegion p2;
    private List<NavmeshRegion> path;

    @Override
    public void mouseDown(int x, int y, int modifiers) {
        super.mouseDown(x, y, modifiers);
        NavmeshRegion p = trySelect(x, y);
        if (p == null) {
            return;
        }

        if (isLeftDown(modifiers)) {
            p1 = p;
        }

        if (isRightDown(modifiers)) {
            p2 = p;
        }

        if (p1 != null && p2 != null) {
            GlobalPathFinder finder = new GlobalPathFinder(p2);
            path = finder.compute(p1);
        }

        redraw();
    }

    @Override
    public void onDraw(InstrumentGraphics g) {
        super.onDraw(g);
        if (p1 != null) {
            g.setColor(Color.RED);
            p1.draw(g);
        }

        if (p2 != null) {
            g.setColor(Color.CYAN);
            p2.draw(g);
        }

        if (path != null && !path.isEmpty()) {
            g.setColor(Color.WHITE);
            for (int i = 0; i < path.size() - 1; i++) {
                NavmeshRegion polygon1 = path.get(i);
                NavmeshRegion polygon2 = path.get(i + 1);
                FPoint center1 = polygon1.getCenter();
                FPoint center2 = polygon2.getCenter();
                g.drawLine(center1.x, center1.y, center2.x, center2.y);
            }
        }
    }
}
